package view;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import view.layers.EventLayer;
import view.layers.Layer;
import view.layers.TerrainLayer;
import view.layers.UserLayer;

/**
 * Processes the inputs received from the user's keypad as events.
 *
 * @author rhaq
 * @version 1.00 2009-05-05 Initial submission.
 */
public class GraphicsIO
{
	/** The background colour of the screen. */
	private static final int BACKGROUND_COLOUR = 0xffffff;

	/** The graphics object which allows manipulation. */
	private Graphics g;

	/** The layer that will contain the map. */
	private Layer terrainLayer;

	/** The layer that the user has control over. */
	private Layer userLayer;
	
	private UserLayer eventLayer;


	/**
	 * Creates an instance of this graphics manipulation class to create layers of the specified
	 * dimensions, and use the specified graphics object to manipulate them.
	 * @param g The graphics object used to manipulate 2D geometric shapes.
	 * @param width The width of the screen to output to.
	 * @param height The height of the screen to output to.
	 * @throws IOException If the graphics for this layer cannot be loaded.
	 */
	public GraphicsIO(Graphics g, int width, int height) throws IOException
	{
		this.g = g;
		this.terrainLayer = new TerrainLayer(width, height);
		this.userLayer = new UserLayer(width, height, terrainLayer);
		this.eventLayer = new EventLayer(width, height, terrainLayer);

		g.setColor(BACKGROUND_COLOUR);
		g.fillRect(0, 0, width, height);
	}


	/**
	 * Processes an event where the user pressed down on the keypad.
	 */
	public void pressedDown()
	{
		userLayer.moveDown();
	}


	/**
	 * Processes an event where the user pressed left on the keypad.
	 */
	public void pressedLeft()
	{
		userLayer.moveLeft();
	}


	/**
	 * Processes an event where the user pressed right on the keypad.
	 */
	public void pressedRight()
	{
		userLayer.moveRight();
	}


	/**
	 * Processes an event where the user pressed up on the keypad.
	 */
	public void pressedUp()
	{
		userLayer.moveUp();
	}


	/**
	 * Updates the graphics on the screen so that any changes are visible.
	 */
	public void updateGraphics()
	{
		terrainLayer.paint(g);
		userLayer.paint(g);
		eventLayer.paint(g);
	}


	/**
	 * Retrieves the 
	 * @return the terrainLayer.
	 */
	public Layer getTerrainLayer()
	{
		return terrainLayer;
	}


	/**
	 * Retrieves the 
	 * @return the userLayer.
	 */
	public Layer getUserLayer()
	{
		return userLayer;
	}


	/**
	 * Retrieves the 
	 * @return the eventLayer.
	 */
	public UserLayer getEventLayer()
	{
		return eventLayer;
	}
}